/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package verins.Utils;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import verins.beans.Persona;
import verins.beans.Personarnv;
import verins.beans.Tipopersona;

/**
 *
 * @author hectdx
 */
public class Comunes {
 
    //recibe el list del query, y llena el combo con objetos
    //para q se muestre la descripcion del objeto, se debe reescribir el toString()
    public static void  llenarCombo(JComboBox combo,List lista){
        if (lista!=null){
            Iterator it =lista.iterator();
            while (it.hasNext()){
                combo.addItem(it.next());
            }
        }
    }
    
    public static void centrarVentana(JFrame vent){
        Toolkit tk=vent.getToolkit();
        Dimension d=tk.getScreenSize();
        int x=(int)(d.getWidth()/2 -vent.getWidth()/2);
        int y=(int) (d.getHeight()/2 -vent.getHeight()/2);
        vent.setLocation(x,y );
    }
    
    public static void centrarVentana(JDialog vent){
        Toolkit tk=vent.getToolkit();
        Dimension d=tk.getScreenSize();
        int x=(int)(d.getWidth()/2 -vent.getWidth()/2);
        int y=(int) (d.getHeight()/2 -vent.getHeight()/2);
        vent.setLocation(x,y );
    }
    
    public static void limpiarTabla(JTable tabla){
        DefaultTableModel dtm=(DefaultTableModel)tabla.getModel();
        while(dtm.getRowCount()>0)
            dtm.removeRow(0);
    }
    
    
    //metodo para setear columnas con tamaño adecuado de acuerdo a los datos
     public static void setearColumnas(JTable tbl){
        int anchoTotal=tbl.getWidth();
        int anchoCol=0;
        int anchoTotalStrings=0;
        
        if (tbl.getRowCount()>0){
            DefaultTableModel dtm=(DefaultTableModel)tbl.getModel();
            int[] anchoCols=new int[dtm.getColumnCount()];
            //proporciones segun data
            for (int i=0;i<dtm.getColumnCount();i++){    
                //System.out.println(tblProcesos.getValueAt(0, i)+" - "+tblProcesos.getValueAt(0, i).toString().length());
                anchoTotalStrings+=tbl.getValueAt(0, i).toString().length();
                anchoCols[i]=tbl.getValueAt(0, i).toString().length();
            }
            //setea tamaños
            for (int i=0;i<dtm.getColumnCount();i++){
              anchoCol=(anchoCols[i]*anchoTotal)/anchoTotalStrings;
              tbl.getColumn(tbl.getColumnName(i)).setPreferredWidth(anchoCol);
            }
        }
    }
    
    public static String getPathImagenes(){
          File directory = new File ("x");
          String ruta="";
             try {
               ruta=directory.getCanonicalPath();
               ruta=ruta.substring(0, ruta.length()-1)+"imagenesPrueba/";
               System.out.println(ruta);
               return ruta;
               
             }catch(Exception e) {
             System.out.println("Exceptione is ="+e.getMessage());
             return "/";
             }
    }
    
    public static String getPathPadrones(){
          File directory = new File ("x");
          String ruta="";
             try {
               ruta=directory.getCanonicalPath();
               ruta=ruta.substring(0, ruta.length()-1)+"Padrones/";
               System.out.println(ruta);
               return ruta;
               
             }catch(Exception e) {
             System.out.println("Exceptione is ="+e.getMessage());
             return "/";
             }
    }
    
    public static String getPathPadronesDigitales(){
          File directory = new File ("x");
          String ruta="";
             try {
               ruta=directory.getCanonicalPath();
               ruta=ruta.substring(0, ruta.length()-1)+"PadronesDigital/";
               System.out.println(ruta);
               return ruta;
               
             }catch(Exception e) {
             System.out.println("Exceptione is ="+e.getMessage());
             return "/";
             }
    }
    
   public static String getPathRNV(){
          File directory = new File ("x");
          String ruta="";
             try {
               ruta=directory.getCanonicalPath();
               ruta=ruta.substring(0, ruta.length()-1)+"RNV/";
               System.out.println(ruta);
               return ruta;
               
             }catch(Exception e) {
             System.out.println("Exceptione is ="+e.getMessage());
             return "/";
             }
    } 
    public static Persona convertirAPersona(Personarnv personarnv){
            Persona persona = new Persona();
            persona.setApellidomaterno(personarnv.getApematerno());
            persona.setApellidopaterno(personarnv.getApepaterno());
            persona.setDni(personarnv.getDni());
            persona.setNombres(personarnv.getNombres());
            persona.setFechanacimiento(personarnv.getFechanac());
            persona.setUbigeo(personarnv.getUbigeo());
            persona.setIdtipopersona(new Tipopersona(3));
            persona.setDni(personarnv.getDni());
            
            return persona;
    }
    
    public static  void  enviarEmail(String emailTo,String asunto,String mensaje){
          try {
                Properties props = new Properties();
                props.setProperty("mail.smtp.host", "smtp.live.com");
                props.setProperty("mail.smtp.starttls.enable", "true");
                props.setProperty("mail.smtp.port", "25");
                props.setProperty("mail.smtp.user", "darth_hibrid@hotmail.com");
                props.setProperty("mail.smtp.auth", "true");

                System.out.println("enviando mail");
                Session session = Session.getDefaultInstance(props);
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress("VERInscribe@hotmail.com"));
                message.addRecipient(
                Message.RecipientType.TO,
                new InternetAddress(emailTo));
                message.setSubject(asunto);
                message.setText(mensaje);
                Transport t = session.getTransport("smtp");
                t.connect("darth_hibrid@hotmail.com", "shadowzone");
                t.sendMessage(message, message.getAllRecipients());
                t.close();

                System.out.println("email enviado");
        }
        catch (Exception ex){
        System.out.println(ex.getMessage());
        }
      }
         
    
    
}
